﻿using HardcoreGame.Common.Abstract;
using HardcoreGame.Common.Cores.Seasons;
using HardcoreGame.Common.Systems;
using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameInput;
using Terraria.Graphics.Effects;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.Players
{
    public class SeasonPlayer : ModPlayer
    {
        private int _breathTimer;
        private const int BreathInterval = 180;
        private const int SweatThreshold = 2;
        private int currentEquipments = 0;
        private int hellfireArmorDamageTimer = 0;
        private const int HellfireDamageInterval = 60;
        private const int HellfireDamageAmount = 10;
        public override void ResetEffects()
        {
            currentEquipments = 0;
        }
        public override void PostUpdateEquips()
        {
            SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
            Season currentSeason = seasonSystem.currentSeason;
            if (currentSeason is Summer)
            {
                CountEquipments();
                if (currentEquipments >= SweatThreshold)
                {
                    ApplyOverheatPenalties();
                }
                CheckHellfireArmor();
            }
            if (currentSeason is Winter)
            {
                bool hasHellfireHelmet1 = Player.armor[0].type == ItemID.FrostHelmet;
                bool hasHellfireBreastplate1 = Player.armor[1].type == ItemID.FrostBreastplate;
                bool hasHellfireLeggings1 = Player.armor[2].type == ItemID.FrostLeggings;
                if (hasHellfireHelmet1 && hasHellfireBreastplate1 && hasHellfireLeggings1)
                {
                    hellfireArmorDamageTimer++;
                    if (hellfireArmorDamageTimer >= HellfireDamageInterval)
                    {
                        Player.statLife -= HellfireDamageAmount;
                        CombatText.NewText(Player.getRect(), CombatText.DamagedHostile, -HellfireDamageAmount);
                        hellfireArmorDamageTimer = 0;
                    }
                }
                else
                {
                    hellfireArmorDamageTimer = 0;
                }
            }
        }

        private void CheckHellfireArmor()
        {
            bool hasHellfireHelmet = Player.armor[0].type == ItemID.MoltenHelmet;
            bool hasHellfireBreastplate = Player.armor[1].type == ItemID.MoltenBreastplate;
            bool hasHellfireLeggings = Player.armor[2].type == ItemID.MoltenGreaves;

            if (hasHellfireHelmet && hasHellfireBreastplate && hasHellfireLeggings)
            {
                hellfireArmorDamageTimer++;
                if (hellfireArmorDamageTimer >= HellfireDamageInterval)
                {
                    Player.statLife -= HellfireDamageAmount;
                    CombatText.NewText(Player.getRect(), CombatText.DamagedHostile, -HellfireDamageAmount);
                    hellfireArmorDamageTimer = 0;
                }
            }
            else
            {
                hellfireArmorDamageTimer = 0;
            }
        }

        public override void PreUpdate()
        {            
            HandleWinterBreathEffect();
        }
        private void HandleWinterBreathEffect()
        {
            SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
            if (seasonSystem.currentSeason is Winter && Player.whoAmI == Main.myPlayer)
            {
                if (Player.ZoneOverworldHeight && Main.rand.NextBool(3))
                {
                    _breathTimer++;
                    if (_breathTimer >= BreathInterval)
                    {
                        SpawnBreathParticles();
                        _breathTimer = 0;
                        _breathTimer = -Main.rand.Next(30);
                    }
                }
                if (Player.velocity.X != 0 || Player.velocity.Y < 0)
                {
                    if (Main.rand.NextBool(60))
                    {
                        SpawnBreathParticles();
                    }
                }
            }
        }

        private void SpawnBreathParticles()
        {
            Vector2 breathPosition = Player.Center + new Vector2(0, -Player.height * 0.25f);
            breathPosition.X += Player.direction * Player.width * 0.4f;
            Vector2 velocity = new Vector2(Player.direction * 0.5f, -0.2f);
            for (int i = 0; i < 3; i++)
            {
                Dust dust = Dust.NewDustPerfect(breathPosition, DustID.Cloud, velocity * Main.rand.NextFloat(0.8f, 1.2f), 100, Color.White * 0.8f, Main.rand.NextFloat(0.8f, 1.5f));
                dust.noGravity = true;
                dust.fadeIn = 1.2f;
                dust.velocity.Y -= Main.rand.NextFloat(0.5f);
            }
        }
        private void CountEquipments()
        {
            if (!Player.armor[0].IsAir) currentEquipments++;
            if (!Player.armor[1].IsAir) currentEquipments++;
            if (!Player.armor[2].IsAir) currentEquipments++;
            if (!Player.armor[10].IsAir) currentEquipments++;
            if (!Player.armor[11].IsAir) currentEquipments++;
            if (!Player.armor[12].IsAir) currentEquipments++;
        }
        public override void PostUpdate()
        {
            if (Player.HasBuff(ModContent.BuffType<HeatStroke>()))
            {
                Filters.Scene.Activate("HeatStroke", default, []);
                Filters.Scene.Activate("HeatStroke2", default, []);
                Filters.Scene.Activate("HeatStroke4", default, []);
                Filters.Scene.Activate("HeatStroke6", default, []);
            }
            else
            {
                Filters.Scene.Deactivate("HeatStroke", []);
                Filters.Scene.Deactivate("HeatStroke2", []);
                Filters.Scene.Deactivate("HeatStroke4", []);
                Filters.Scene.Deactivate("HeatStroke6", []);
            }
            if (Player.HasBuff(ModContent.BuffType<Hypothermia>()))
            {
                Filters.Scene.Activate("Hypothermia", default, []);
            }
            else
            {
                Filters.Scene.Deactivate("Hypothermia", []);
            }
        }
        private void ApplyOverheatPenalties()
        {
            int penaltyLevel = currentEquipments - SweatThreshold + 1;
            if (Main.myPlayer == Player.whoAmI && Main.rand.NextBool(8 - penaltyLevel * 2))
            {
                Dust.NewDust(Player.position, Player.width, Player.height,
                    DustID.Water, 0f, -2f, 100, default, 1.2f);
            }
            if (penaltyLevel >= 2)
            {
                Player.moveSpeed -= 0.3f * (penaltyLevel - 1);
                Player.maxRunSpeed -= 0.3f * (penaltyLevel - 1);
                if (penaltyLevel >= 3)
                {
                    Player.velocity *= 0.5f;
                }
            }
        }
        public override void ProcessTriggers(TriggersSet triggersSet)
        {
            if (KeybindSystem.SeasonChange.JustPressed)
            {
                SeasonUISystem.ToggleUI();
            }
        }

    }
}